﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;

namespace Eniux.Windows.EnterpriseLibrary.Commands
{
    /// <summary>
    /// 属性命令触发器
    /// </summary>
    public class PropertyCommandTrigger : CommandTrigger
    {
        #region 依赖属性

        #region Property

        /// <summary>
        /// Property属性
        /// </summary>
        public static readonly DependencyProperty PropertyProperty =
            DependencyProperty.Register("Property", typeof(DependencyProperty), typeof(PropertyCommandTrigger),
            new FrameworkPropertyMetadata(null));

        /// <summary>
        /// 获取或设置Property属性的值
        /// </summary>
        public DependencyProperty Property
        {
            get { return (DependencyProperty)GetValue(PropertyProperty); }
            set { SetValue(PropertyProperty, value); }
        }

        #endregion

        #region Value

        /// <summary>
        /// Value属性
        /// </summary>
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(PropertyCommandTrigger),
            new FrameworkPropertyMetadata(null));

       /// <summary>
        /// 获取或设置Value属性的值
       /// </summary>
        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        #endregion

        #region T 属性

        /// <summary>
        /// T属性
        /// </summary>
        public static readonly DependencyProperty TProperty =
            DependencyProperty.Register("T", typeof(object), typeof(PropertyCommandTrigger),
            new FrameworkPropertyMetadata(null, OnTChanged));

        /// <summary>
        /// 获取或设置T属性的值
        /// </summary>
        public object T
        {
            get { return (object)GetValue(TProperty); }
            set { SetValue(TProperty, value); }
        }

        static void OnTChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion

        #endregion

        #region 重写方法

        protected override Freezable CreateInstanceCore()
        {
            return new PropertyCommandTrigger();
        }

        protected override void InitializeCore(FrameworkElement source)
        {
            DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(Property, source.GetType());
            descriptor.AddValueChanged(source, (s, e) =>
            {
                CommandParameter<object> parameter = new PropertyCommandParameter<object, object>(
                    CommandParameter, Property, source.GetValue(Property));

                object value = Value;
                if (descriptor.Converter.CanConvertFrom(typeof(string)))
                {
                    value = descriptor.Converter.ConvertFromString(Value);
                }

                if (object.Equals(source.GetValue(Property), value))
                    ExecuteCommand(parameter);
            });
        } 

        #endregion
    }
}
